#include<stdio.h>
#include "../include/vfs.h"
#include<stdlib.h>
#include<string.h>

int deletedir(char path[])
{
	FD *fd,*exist;
	dir_empty=0;
	int i=0;
	FILE *fp;
	long saveblock;
	dir_empty=0;

	if(strcmp(path,"/")==0)
		return 5;

	fp = fopen(filesystem_name,"rb+");

	header = malloc(sizeof(Header));
	
	fread(header,sizeof(Header),1,fp);

	

	if(path[strlen(path)-1]=='/' && strlen(path)>2)
	{
		path[strlen(path)-1]='\0';

	}	

	fd=(FD*)malloc(sizeof(FD));
	strcpy(fd->path,path);

	exist=(FD*)malloc(sizeof(FD));
	strcpy(exist->path,path);

		exist = search(roots.naryroot,exist);


		if(exist==NULL)
			return 1;

		else if(dir_empty==1)
		{
			return 2;
		}
		roots.naryroot=delete_root(roots.naryroot,fd);
		hash_deletion(fd);		
		delete_bst(fd->path,roots.bstRoot);

		if(exist->file_type>0)
		{
			saveblock = exist->block_no;
			
			for(i=0;i<header->num_blocks;i++)
					{
						if(header->freelist[i].block==saveblock)
						{

							header->freelist[i].isfree=1;
							
							break;
						}
					}
		}

	rewind(fp);
	fwrite(header,sizeof(Header),1,fp);

	fclose(fp);
	
	return 0;

}


	
